/**
 * Created by IntelliJ IDEA.
 * User: user
 * Date: 05.06.12
 * Time: 15:05
 * To change this template use File | Settings | File Templates.
 */
package zakladki.AS3 {
import flash.events.Event;

import mx.controls.Alert;
import mx.core.FlexGlobals;
import mx.core.UIComponent;

import zakladki.decorators.HelperUIDecorator;

public class ToolTipHelper {

    private var mainParentComponent:UIComponent;

    private var displayedComponentMap:Object = new Object();

    private var switcher:int = 0;
    private var posXa:Number = -1;
    private var posYa:Number = -1;
    private var posXb:Number = -1;
    private var posYb:Number = -1;
    private var help:String = "";

    private var helpDisplayed:Boolean = false;

    private var mainApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    public function ToolTipHelper(uiComp:UIComponent) {
        this.mainParentComponent = uiComp;
    }

    public function displayHelp(helperUIComponent:UIComponent,helpString:String):void{
         help = helpString;

         var compKey:String = getUIComponentKey(helperUIComponent);
         if(!displayedComponentMap.hasOwnProperty(compKey)){
             displayedComponentMap[compKey] = new HelperUIDecorator(helperUIComponent);
         }
         var helperUIDecorator:HelperUIDecorator = displayedComponentMap[compKey];
         if(help != ""){
            if(!helperUIDecorator.displayed){
                mainApp.startToolTip(help);
                helperUIDecorator.displayed = true;
            }
         }
         else {
            helperUIDecorator.displayed = false;
            mainApp.closeBigTip();
         }
    }

    public function setHelp(set_string:String):void {
        help = set_string;

        if (help != "") {
            mainParentComponent.addEventListener(Event.ENTER_FRAME, checkMovement);
        }
        else {
            mainParentComponent.removeEventListener(Event.ENTER_FRAME, checkMovement);
            mainApp.closeBigTip();
        }
    }

    private function getUIComponentKey(uiCom:UIComponent):String {
        if(uiCom.id != null){
           return uiCom.id + uiCom.uid;
        }
        return uiCom.uid;
    }

    private function checkMovement(e:Event):void {
        if (help != "") {

            mainApp.newtoolTip.txtHelpSmall.tryScroll();

            if (switcher == 0) {
                posXa = mainParentComponent.mouseX;
                posYa = mainParentComponent.mouseY;
                switcher = 1;
            }
            else {
                posXb = mainParentComponent.mouseX;
                posYb = mainParentComponent.mouseY;
                switcher = 0;
            }

            if (posXa == posXb && posYa == posYb) {
                mainApp.startToolTip(help);
            }
            else {
                mainApp.destroyBigTip();
            }
        }

    }
}
}
